﻿//	Ordinaire.Model.Base
//	Copyright (c) 2011 Jonathan Loe
//
//	MIT license (http://en.wikipedia.org/wiki/MIT_License)
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;

using Ordinaire.Db.Linq;

namespace Ordinaire.Model
{
    /// <summary>
    /// Represents the base of any model class. T represents type of id whereas R represents type of creator and updator.
    /// </summary>
    public abstract class Base<T, R> : IUnique<T>, ITracked<R>
    {
        #region Variables

        /// <summary>
        /// Retained id
        /// </summary>
        protected T _id;

        /// <summary>
        /// Retained Creator
        /// </summary>
        protected R _createdBy = default(R);

        /// <summary>
        /// Retained Created Date
        /// </summary>
        protected Nullable<DateTime> _createdOn = default(DateTime);

        /// <summary>
        /// Retained Updator
        /// </summary>
        protected R _updatedBy = default(R);

        /// <summary>
        /// Retained Update Date
        /// </summary>
        protected Nullable<DateTime> _updatedOn = default(DateTime);

        #endregion

        #region Inherited interfaces

        /// <summary>
        /// Get or set unique id
        /// </summary>
        public T Id
        {
            get { return this._id; }
            set { this._id = value; }
        }

        /// <summary>
        /// Get or set creator
        /// </summary>
        public R CreatedBy
        {
            get { return this._createdBy; }
            set { this._createdBy = value; }
        }

        /// <summary>
        /// Get or set creation date
        /// </summary>
        public Nullable<DateTime> CreatedOn
        {
            get { return (_createdOn.HasValue && _createdOn.Value.Ticks > 0) ? _createdOn : DateTime.Now; }
            set { _createdOn = value; }
        }

        /// <summary>
        /// Get or set updator
        /// </summary>
        public R UpdatedBy
        {
            get { return this._updatedBy; }
            set { this._updatedBy = value; }
        }

        /// <summary>
        /// Get or set update date
        /// </summary>
        public Nullable<DateTime> UpdatedOn
        {
            get { return (_updatedOn.HasValue && _updatedOn.Value.Ticks > 0) ? _updatedOn : null; }
            set { _updatedOn = value; }
        }

        #endregion
    }
}
